/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Com.Entity;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Ardha
 */
public class Kamar {

    private int no_Kamar;
    private String jenis_Kamar;
    private int harga_Sewa;
    private int status_Sewa;
//    private int status_Booking;
    private Connection konek = null;
    private static Kamar kamarKoneksi;

    public Kamar() {
    }

    public Kamar(Connection konek) {
        this.konek = konek;
    }

    public static Kamar getKoneksiKontrol() throws SQLException, FileNotFoundException, IOException {
        if (kamarKoneksi == null) {
            kamarKoneksi = new Kamar(Koneksi.getDBConnection());
        }
        return kamarKoneksi;
    }

    /**
     * @return the no_Kamar
     */
    public int getNo_Kamar() {
        return no_Kamar;
    }

    /**
     * @param no_Kamar the no_Kamar to set
     */
    public void setNo_Kamar(int no_Kamar) throws Exception {
        if (no_Kamar <= 0) {
            throw new Exception("Nomor Kamar salah, nomor kamar tidak bernilai 0 atau negatif");
        } else {
            this.no_Kamar = no_Kamar;
        }

    }

    /**
     * @return the jenis_Kamar
     */
    public String getJenis_Kamar() {
        return jenis_Kamar;
    }

    /**
     * @param jenis_Kamar the jenis_Kamar to set
     */
    public void setJenis_Kamar(String jenis_Kamar) throws Exception {
        if (jenis_Kamar.matches("")) {
            throw new Exception("Jenis Kamar belum diisi");
        } else if (jenis_Kamar.toCharArray().length > 10) {
            throw new Exception("Jenis kamar terlalu panjang, lebih dari 10");
        } else if (!jenis_Kamar.matches("[a-zA-Z]*")) {
            throw new Exception("Jenis kamar salah, terdapat angka atau symbol");
        } else {
            this.jenis_Kamar = jenis_Kamar;
        }
    }

    /**
     * @return the harga_Sewa
     */
    public int getHarga_Sewa() {
        return harga_Sewa;
    }

    /**
     * @param harga_Sewa the harga_Sewa to set
     */
    public void setHarga_Sewa(int harga_Sewa) throws Exception {
        if (harga_Sewa <= 0) {
            throw new Exception("Harga sewa salah, harga sewa tidak bernilai 0 atau negatif");
        } else {
            this.harga_Sewa = harga_Sewa;
        }
    }

    /**
     * @return the status_Sewa
     */
    public int getStatus_Sewa() {
        return status_Sewa;
    }

    /**
     * @param status_Sewa the status_Sewa to set
     */
    public void setStatus_Sewa(int status_Sewa) throws Exception {
        if (status_Sewa >= 0 && status_Sewa <= 1) {
            this.status_Sewa = status_Sewa;
        } else {
            throw new Exception("Status Kamar salah, status Kamar hanya bernilai 0 dan 1");
        }
    }
//
//    /**
//     * @return the status_Booking
//     */
//    public int getStatus_Booking() {
//        return status_Booking;
//    }
//
//    /**
//     * @param status_Booking the status_Booking to set
//     */
//    public void setStatus_Booking(int status_Booking) throws Exception {
//        if (status_Booking >= 0 && status_Booking <= 1) {
//            this.status_Booking = status_Booking;
//        } else {
//            throw new Exception("Status Booking salah, status Kamar hanya bernilai 0 dan 1");
//        }
//    }

    public List<Kamar> getKamar(Kamar kamar) throws SQLException, Exception {
        PreparedStatement psmt = null;
        ResultSet rset = null;
        konek.setAutoCommit(false);
        String sql = "select * from kamar where jeniskamar=(?) and statussewa=(?)";
        psmt = konek.prepareStatement(sql);
        psmt.setObject(1, kamar.getJenis_Kamar());
        psmt.setObject(2, 0);
        rset = psmt.executeQuery();
        List<Kamar> resultKamar = new ArrayList<Kamar>();
        while (rset.next()) {
            Kamar kam = new Kamar();
            kam.setNo_Kamar(rset.getInt(1));
            kam.setJenis_Kamar(rset.getString(2));
            kam.setHarga_Sewa(rset.getInt(3));
            kam.setStatus_Sewa(rset.getInt(4));
//            kam.setStatus_Booking(rset.getInt(5));
            resultKamar.add(kam);
        }
        konek.commit();
        if(resultKamar.isEmpty()){
            throw new Exception("Kamar tidak tersedia");
        }else{
        return resultKamar;
        }
    }
}
